// 引入插件
var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin')
var ExtractTextPlugin = require('extract-text-webpack-plugin')
var OpenBrowserPlugin = require('open-browser-webpack-plugin')

// 指定工程路径
module.exports = {
    devtool: 'cheap-module-eval-source-map',
    entry: { // 入口
        index: './src/app.js',
        vendor: ['jquery', 'moment', 'react', 'react-dom', 'react-router', 'antd', 'react-redux', 'redux']
    },
    output: {// 出口
        path: __dirname + '/build', // __dirname 表示根路径
        publicPath: '/',
        filename: '[name].[chunkHash:8].js', // 文件名
        // filename: 'app_[hash].js'
        chunkFilename: "[name].[chunkHash:8].js",
    },
    devServer: {
        contentBase: './build',
        host: 'localhost',
        inline:true,
        historyApiFallback: true, // 浏览器的返回功能
        port: 8000,
        //host: '192.168.199.81',
        proxy: {
            //开发环境
            // '/crm/api/*': {
            //     target: 'http://dev.hbbcare.com:8087/crm/api',
            //     pathRewrite: {
            //         '^/crm/api': ''
            //     },
            //
            //     changeOrigin: true
            // },
              //田春阳
             //'/crm/api/*': {
             //  target: 'http://192.168.199.123:8087/crm/api',
             //  pathRewrite: {
             //    '^/crm/api': ''
             //  },
             //  changeOrigin: true
             //},
            '/crm/api/*': {
             target: 'http://127.0.0.1:8087/crm/api',
             pathRewrite: {
               '^/crm/api': ''
             },
             changeOrigin: true
            },

        }
    },

    module: { // 加载器
        loaders: [
            {
                test: /\.(js|jsx)$/,
                exclude: [/node_modules/, /component_dev/],
                loader: 'babel-loader',
            }, {
                test: /\.css$/,
                loader: 'style-loader!css-loader',
                // loader: ExtractTextPlugin.extract(['css', 'postcss'])
            }, {
                test: /\.scss$/,
                loader: 'style-loader!css-loader!sass-loader',
                // loader: ExtractTextPlugin.extract(['css', 'postcss', 'sass'])
            }, {
                test: /\.less$/,
                loader: 'style-loader!css-loader!less-loader',
                // loader: ExtractTextPlugin.extract(['css', 'postcss', 'less'])
             },
             {
                test: /\.(png|jpg|jpeg|gif)$/i,
                loader: 'url-loader?name=[hash:8].[ext]&limit=8192',
            },
            {
                //html模板加载器，可以处理引用的静态资源，默认配置参数attrs=img:src，处理图片的src引用的资源
                //比如你配置，attrs=img:src img:data-src就可以一并处理data-src引用的资源了，就像下面这样
                test: /\.html$/,
                loader: "html?attrs=img:src img:data-src"
            }, {
                //文件加载器，处理文件静态资源
                test: /\.(woff|woff2|ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
                loader: 'file-loader?name=./fonts/[name].[ext]'
            }, {
                test: /\.json$/,
                loader: 'json-loader'
            }

        ]
    },

    plugins: [
        new webpack.DefinePlugin({
            'process.env': {
                'NODE_ENV': JSON.stringify("production")
            }
        }),
        new webpack.NoEmitOnErrorsPlugin(),
        new webpack.optimize.CommonsChunkPlugin({
            names: ['vendor', 'manifest'],
            minChunks: Infinity,
        }),
        new webpack.optimize.UglifyJsPlugin({ // 压缩插件
            compress: {
                warnings: false
            },
            output: {
                comments: false
            }
        }),
        new HtmlWebpackPlugin({ // 
            filename: 'index.html', //生成的html存放路径，相对于path
            template: './src/index.ejs', //html模板路径
            title: 'Power',
            inject: true, //js插入的位置，true/'head'/'body'/false
            hash: true, //为静态资源生成hash值
            favicon: './src/img/favicon.ico',
            minify: { //压缩HTML文件
                removeComments: true, //移除HTML中的注释
                collapseWhitespace: false //删除空白符与换行符
            },
            chunksSortMode: 'dependency',
            externalsAssets: {}
        }),
        new webpack.ProvidePlugin({
            $: 'jquery',
            jQuery: 'jquery',
            'windows.jQuery': 'jquery',
        }),
        new ExtractTextPlugin({ // 
            // filename: 'app_[hash].css',
            filename: 'app.css',
            disable: false,
            allChunks: true
        }),
    ],

}
